/*
 * maps : id, name, zoom, posX, posY
 * mapLocs : id, name, address, posX, posY, avgRating, topicCount, discountCount
 */

var MapCtrl = dojo.declare(null, {
	//constructor/destructor
	constructor : mapCtrl_constructor,
	destroy : mapCtrl_destructor,
	
	//methods
	init : mapCtrl_init,
	showMap : mapCtrl_showMap,
	showMapLocation : mapCtrl_showMapLocation
});

function mapCtrl_constructor(){
	var obj = this;
	
	obj._curMapLoc = new google.maps.LatLng(31.246386, 121.475372);
	obj._curZoom = 10;
	obj._curProcess = 0;
	obj._curMapId = parseInt(getCookie("homepage.map.id"));
	obj._gMap = null;
	obj._leftNavList = new TabList();
	obj._maps = new Array();
	obj._mapLocs = new Array();
	obj._gMapMarkers = new Array();
	
	obj._showingMap = false;
}

function mapCtrl_destructor(){
}

function mapCtrl_init(){
	var obj = this;
	
	$(".page_column_right").animate({top : Math.max($(window).scrollTop() + ($(window).height() - $(".page_column_right").height()) / 2, 130)}, {duration : 0 , queue : false});
	$(window).scroll(function(){
		$(".page_column_right").animate({top : Math.max($(window).scrollTop() + ($(window).height() - $(".page_column_right").height()) / 2, 130)}, {duration : 600 , queue : false});
	});
	
	var obj = this;
	if ($("#map_locs:visible").size() > 0) $("#map_locs:visible").effect("drop", {mode : "hide"});
	obj._maps = $.parseJSON($(".page_variable div[var_name='school_maps']").html());
	for (var i = 0; i < obj._maps.length; i ++){
		$("#map_nav").append("<div id='map_item_" + i + "' class='tab tab_padding' alt='" + i + "'><div class='normal'><a href='javascript:void(0)'>" + obj._maps[i].name + "</a></div></div>");
		var item = $("#map_item_" + i);
		item.click(function(){
			var id = $(this).attr("alt");
			obj.showMap(id);
		});
	}
	obj.showMap();
	
	$("#search_bar #search_bar_button").click(function(){
		window.location = "/search?type=1&q=" + $("#search_bar input").val();
	});
}

function mapCtrl_showMap(mapId){
	var obj = this;
	if (obj._showingMap) return;
	obj._showingMap = true;
	if (mapId == null){
		if (isNaN(obj._curMapId) || obj._curMapId < 0 || obj._curMapId >= obj._maps.length) obj._curMapId = 0;
		mapId = obj._curMapId;
	}  else {
		if (obj._curMapId == mapId) return;
		if (mapId < 0 || mapId > obj._maps.length) mapId = 0;
		obj._curMapId = mapId;
	}
	var curMap = obj._curMapId;
	
	$("#map_nav .tab div").removeClass("selected");
	$("#map_nav .tab div").addClass("normal");
	$("#map_nav #map_item_" + mapId + " div").removeClass("normal");
	$("#map_nav #map_item_" + mapId + " div").addClass("selected");
	
	setCookie("homepage.map.id", mapId, 365);
	if ($("#map_locs:visible").size() > 0) $("#map_locs:visible").effect("drop", {mode : "hide"}, loadList);
	else loadList();
	function loadList(){
		if (curMap != obj._curMapId) return;
		obj.showMapLocation(new google.maps.LatLng(obj._maps[mapId].posX, obj._maps[mapId].posY), obj._maps[mapId].zoom);
		$("#map_canvas").fadeIn(function(){
			if (curMap != obj._curMapId) return;
			$("#map_locs").html("");
			$.getJSON("/map/getSchoolMapLocByGoogleMapId", {"id" : obj._maps[mapId].id}, function(data){
				if (curMap != obj._curMapId) return;
				var itemHtml = $("#map_loc_item_example").html();
				obj._mapLocs = data;
				for (var i = 0; i < obj._mapLocs.length; i ++){
					$("#map_locs").append("<li id='map_loc_item_" + i + "' style='float:left;'>" + itemHtml + "</li>");
					var item = $("#map_locs #map_loc_item_" + i);
					item.find("#item_bound").attr("alt", i);
					item.find("#item_bound").mouseout(function(){
						for (var j = 0; j < obj._mapLocs.length; j ++)
							if (!obj._gMapMarkers[j].getVisible()) obj._gMapMarkers[j].setVisible(true);
					}).mouseover(function(){
						var id = $(this).attr("alt");
						for (var j = 0; j < obj._mapLocs.length; j ++){
							var type = (j == id) ? true : false;
							if (obj._gMapMarkers[j].getVisible() != type) obj._gMapMarkers[j].setVisible(type);
						}
					}).click(function(){
						var id = $(this).attr("alt");
						window.location = "/map/maploc?id=" + obj._mapLocs[id][0];
					});
					item.find("#name").html(obj._mapLocs[i][1]);
					item.find("#address").html(obj._mapLocs[i][2]);
					item.find("#topic_count").html(obj._mapLocs[i][6]);
					item.find("#discount_count").html(obj._mapLocs[i][7]);
					item.find("#link_enter").attr("href", "/map/maploc?id=" + obj._mapLocs[i][0]);
					if (parseFloat(obj._mapLocs[i][5]) > 0.0){
						item.find("#loc_rating").css("display", "");
						item.find("#loc_rating").html("<div class='current-rating' style='width:" + 15 * parseFloat(obj._mapLocs[i][5]) + "px;'></div>");
					}  else item.find("#loc_rating").css("display", "none");
					var marker;
					if (i >= obj._gMapMarkers.length){
						marker = new google.maps.Marker();
						obj._gMapMarkers.push(marker);
					}  else marker = obj._gMapMarkers[i];
					marker.setMap(obj._gMap);
					marker.setPosition(new google.maps.LatLng(obj._mapLocs[i][3], obj._mapLocs[i][4]));
					marker.setTitle(obj._mapLocs[i][1]);
					marker.setVisible(true);
				}
				for (var i = obj._mapLocs.length; i < obj._gMapMarkers.length; i ++) obj._gMapMarkers[i].setVisible(false);
				$("#map_locs").effect("drop", {mode : "show"});
				obj._showingMap = false;
			});
		});
	}
}

function mapCtrl_showMapLocation(mapLoc, zoom){
	var obj = this;
	if (mapLoc == null) mapLoc = obj._curMapLoc;
	else obj._curMapLoc = mapLoc;
	if (zoom == null) zoom = obj._curZoom;
	else obj._curZoom = zoom;
    if (obj._gMap == null){
	    var myOptions = {
			zoom : zoom,
			center : mapLoc,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl : false,
			navigationControlOptions : {
				style : google.maps.NavigationControlStyle.SMALL
			}
	    }
		obj._gMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	}  else {
		obj._gMap.setCenter(mapLoc);
		obj._gMap.setZoom(zoom);
	}  
}

if (dojo.getObject("mapCtrl") == null) mapCtrl = new MapCtrl();
$(document).ready(function(){
	mapCtrl.init();
});